package com.jie.deletefile.MicroPhone;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

public class MicroPhoneActivity extends AppCompatActivity implements View.OnClickListener {

    private android.widget.Button btnplay;
    private android.widget.Button btndis;
    private android.widget.LinearLayout activitymicrophone;
    private static final int REQUEST_CODE = 1;
    private static final String[] PERMISSION = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };
    private CheckPermission checkPermission;
    private boolean hadpermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_micro_phone);
        this.activitymicrophone = (LinearLayout) findViewById(R.id.activity_micro_phone);
        this.btndis = (Button) findViewById(R.id.btn_dis);
        this.btnplay = (Button) findViewById(R.id.btn_play);
        btnplay.setOnClickListener(this);
        btndis.setOnClickListener(this);
        checkPermission = new CheckPermission(MicroPhoneActivity.this);
        if (checkPermission.permissionSet(PERMISSION)) {
            startPermissionActivity();
            hadpermission = false;
        } else {
            hadpermission = true;
        }


    }

    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(MicroPhoneActivity.this, REQUEST_CODE, PERMISSION);
    }

    @Override
    public void onClick(View v) {
        if (!hadpermission) {
            Toast.makeText(this, "缺少权限", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()) {
            case R.id.btn_play:
                Intent intent = new Intent(MicroPhoneActivity.this, MyService.class);
                startService(intent);

                break;
            case R.id.btn_dis:
                Intent intent1 = new Intent(MicroPhoneActivity.this, MyService.class);
                stopService(intent1);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            hadpermission = false;

        } else {
            hadpermission = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(MicroPhoneActivity.this, MyService.class);
        stopService(intent);
    }
}
